<template>
    <div class="container">
        <div class="row col-md-8 col-md-offset-3">


            <div class="form-horizontal">


                <div class="form-group">
                    <label  class="col-sm-2 control-label">用户名</label>
                    <div class="col-sm-5">
                        <input type="text"
                               name="email"
                               class="form-control" required v-model="email" placeholder="请输入邮箱">
                    </div>
                </div>
                <div class="form-group">
                    <label  class="col-sm-2 control-label">密&nbsp&nbsp&nbsp&nbsp&nbsp码</label>
                    <div class="col-sm-5">
                        <input type="password"
                               name="password"
                               class="form-control" v-model="password" placeholder="请输入密码">
                    </div>
                </div>

                <div class="form-group">
                    <div class="col-sm-offset-1 col-sm-6" style="margin-top: 40px;">
                        <button type="button" class="btn btn-default btn-success" style="float: left;margin-left: -15px;" @click="wl_login()" >登录</button>
                        <a type="button" class="btn btn-default btn-success" 
                        style="float: right;color:white;" href="/#/register">注册一个</a>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
import conf from '../layout/conf.vue'//引用模块进来
    export default {
        mounted() {
        this.ymh();
        },
        data(){
            return {
                list: {},
                email:"",
                password:"",
                url:conf.url+"post/web/index.php?r=user/login/index",

            }


        },

        methods:{
            wl_login(){


                var params = new URLSearchParams();
                params.append('user_email',this.email);
                params.append('user_pwd', this.password);

                this.axios.post(this.url,params).then(response=>{
                    
                     var info=response['data']['data'];
                     console.log(info)
                    let  error=response.data['error'];
                    if(error==0){
                        //登陆成功!
                        console.log(response);
                        message("ok","登陆成功!");
                        this.SetCookie("wlphp.cn", "", -1);
                        this.SetCookie("name", "", -1);
                        this.SetCookie("tou", "", -1);
            
                        this.SetCookie("wlphp.cn",info['access_token'],'1');
                        this.SetCookie("name",info['username'],'1');
                        this.SetCookie("tou",info['tou'],'1');
                         setTimeout(function () {
                                location.href="/";
                            },1000)
                        

                    }else{
                        //登陆失败!
                         this.message("no","登陆失败!");
                    }

                });
            },

            wl_test(){

            this.message("no","登陆失败!");
            },



                 getCookie: function (cname) {
                var name = cname + "=";
                var ca = document.cookie.split(';');
                for (var i = 0; i < ca.length; i++) {
                    var c = ca[i];
                    while (c.charAt(0) == ' ') c = c.substring(1);
                    if (c.indexOf(name) != -1) return c.substring(name.length, c.length);
                }
                return "";
            },

             

                

        }


    }


    function message(type,data) {

        if(type=="ok"){
            $("#ok").html(data);
            $("#ok").fadeIn();
            setTimeout(function () {
                $("#ok").fadeOut();
            },1000)
        }else{
            $("#no").html(data);
            $("#no").fadeIn();
            setTimeout(function () {
                $("#no").fadeOut();
            },1000)
        }

    }
</script>
